<template>
	<a-card :bordered="false">
		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col :span="6">
					<a-form-item label="水表编号" name="metersId">
						<a-input v-model:value="searchFormState.metersId" placeholder="请输入水表编号" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="水表名称" name="metersName">
						<a-input v-model:value="searchFormState.metersName" placeholder="请输入水表名称" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="水表型号" name="metersTypeid">
						<a-input v-model:value="searchFormState.metersTypeid" placeholder="请输入水表型号" />
					</a-form-item>
				</a-col>
				<a-col :span="6" v-show="advanced">
					<a-form-item label="组装日期" name="timeAssembly">
						<a-range-picker v-model:value="searchFormState.timeAssembly" show-time />
					</a-form-item>
				</a-col>
				<a-col :span="6" v-show="advanced">
					<a-form-item label="使用状态(0:仓库1:地方仓库2:运行3:维修4:报废)" name="status">
						<a-select v-model:value="searchFormState.status" placeholder="请选择使用状态(0:仓库1:地方仓库2:运行3:维修4:报废)" :options="statusOptions" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-button type="primary" @click="tableRef.refresh()">查询</a-button>
					<a-button style="margin: 0 8px" @click="reset">重置</a-button>
					<a @click="toggleAdvanced" style="margin-left: 8px">
						{{ advanced ? '收起' : '展开' }}
						<component :is="advanced ? 'up-outlined' : 'down-outlined'"/>
					</a>
				</a-col>
			</a-row>
		</a-form>
		<s-table
			ref="tableRef"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
			:row-selection="options.rowSelection"
		>
			<template #operator class="table-operator">
				<a-space>
					<a-button type="primary" @click="formRef.onOpen()" v-if="hasPerm('metersInfoAdd')">
						<template #icon><plus-outlined /></template>
						新增
					</a-button>
					<xn-batch-delete
						v-if="hasPerm('metersInfoBatchDelete')"
						:selectedRowKeys="selectedRowKeys"
						@batchDelete="deleteBatchMetersInfo"
					/>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'status'">
					{{ $TOOL.dictTypeData('status_type', record.status) }}
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a @click="formRef.onOpen(record)" v-if="hasPerm('metersInfoEdit')">编辑</a>
						<a-divider type="vertical" v-if="hasPerm(['metersInfoEdit', 'metersInfoDelete'], 'and')" />
						<a-popconfirm title="确定要删除吗？" @confirm="deleteMetersInfo(record)">
							<a-button type="link" danger size="small" v-if="hasPerm('metersInfoDelete')">删除</a-button>
						</a-popconfirm>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="tableRef.refresh()" />
</template>

<script setup name="metersInfo">
import tool from '@/utils/tool'
import { cloneDeep } from 'lodash-es'
import Form from './form.vue'
import metersInfoApi from '@/api/productmanagement/metersInfoApi'
const searchFormState = ref({})
const searchFormRef = ref()
const tableRef = ref()
const formRef = ref()
const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
// 查询区域显示更多控制
const advanced = ref(false)
const toggleAdvanced = () => {
	advanced.value = !advanced.value
}
const columns = [
	{
		title: '水表编号',
		dataIndex: 'metersId'
	},
	{
		title: '水表名称',
		dataIndex: 'metersName'
	},
	{
		title: '水表型号',
		dataIndex: 'metersTypeid'
	},
	{
		title: '组装日期',
		dataIndex: 'timeAssembly'
	},
	{
		title: '组装人员',
		dataIndex: 'perAssembly'
	},
	{
		title: '安装日期',
		dataIndex: 'installTime'
	},
	{
		title: '安装地点',
		dataIndex: 'installLocal'
	},
	{
		title: '安装人员',
		dataIndex: 'perInstall'
	},
	{
		title: '程序录入时间',
		dataIndex: 'timeInput'
	},
	{
		title: '程序录入人员',
		dataIndex: 'perInput'
	},
	{
		title: '维修时间',
		dataIndex: 'timeRepair'
	},
	{
		title: '维修人员',
		dataIndex: 'perRepair'
	},
	{
		title: '报废日期',
		dataIndex: 'timeDamage'
	},
	{
		title: '报废人员',
		dataIndex: 'perDamage'
	},
	{
		title: '使用状态',
		dataIndex: 'status'
	},
	// {
	// 	title: '可选排序字段',
	// 	dataIndex: 'metersOrder'
	// },
]
// 操作栏通过权限判断是否显示
if (hasPerm(['metersInfoEdit', 'metersInfoDelete'])) {
	columns.push({
		title: '操作',
		dataIndex: 'action',
		align: 'center',
		width: 150
	})
}
const selectedRowKeys = ref([])
// 列表选择配置
const options = {
	// columns数字类型字段加入 needTotal: true 可以勾选自动算账
	alert: {
		show: true,
		clear: () => {
			selectedRowKeys.value = ref([])
		}
	},
	rowSelection: {
		onChange: (selectedRowKey, selectedRows) => {
			selectedRowKeys.value = selectedRowKey
		}
	}
}
const loadData = (parameter) => {
	const searchFormParam = cloneDeep(searchFormState.value)
	// timeAssembly范围查询条件重载
	if (searchFormParam.timeAssembly) {
		searchFormParam.startTimeAssembly = searchFormParam.timeAssembly[0]
		searchFormParam.endTimeAssembly = searchFormParam.timeAssembly[1]
		delete searchFormParam.timeAssembly
	}
	return metersInfoApi.metersInfoPage(Object.assign(parameter, searchFormParam)).then((data) => {
		return data
	})
}
// 重置
const reset = () => {
	searchFormRef.value.resetFields()
	tableRef.value.refresh(true)
}
// 删除
const deleteMetersInfo = (record) => {
	let params = [
		{
			id: record.id
		}
	]
	metersInfoApi.metersInfoDelete(params).then(() => {
		tableRef.value.refresh(true)
	})
}
// 批量删除
const deleteBatchMetersInfo = (params) => {
	metersInfoApi.metersInfoDelete(params).then(() => {
		tableRef.value.clearRefreshSelected()
	})
}
const statusOptions = tool.dictList('status_type')
</script>
